<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

/**
 * 老用户回归送礼物表
 */
class Rebate extends Base
{
    public $table = 'rebate_send_log'; //发放领取记录表
    protected $giftStream = 'gift_stream';


    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    //获取用户流水值
    public function getGiftData($userId, $startTime, $endTime){
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $res = DB::table($this->giftStream)
                ->selectRaw(DB::raw('SUM(gift_num * gift_price) as total'))
                ->where('from_user_id',$userId)
                ->where('type',2)
                ->orWhere('from_gift_type',5)
                ->where('change_timestamp','>=',$startTime)
                ->where('change_timestamp','<=',$endTime)
                ->first();
        $res = self::objToArray($res);
        return $res;    
    }

    //获取用户送礼背包流水
    public function getAllBagData($userId, $startTime, $endTime){
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $res = DB::table($this->giftStream)
                ->selectRaw(DB::raw('SUM(gift_num * gift_price) as total'))
                ->where('from_user_id',$userId)
                ->where('type',2)
                ->where('change_timestamp','>=',$startTime)
                ->where('change_timestamp','<=',$endTime)
                ->first();
        $res = self::objToArray($res);
        return $res;    
    }

    //获取用户送礼彩蛋流水
    public function getAllColorGiftData($userId, $startTime, $endTime){
        $startTime = strtotime($startTime);
        $endTime = strtotime($endTime);
        $res = DB::table($this->giftStream)
                ->selectRaw(DB::raw('SUM(gift_num * gift_price) as total'))
                ->where('from_user_id',$userId)
                ->where('from_gift_type',5)
                ->where('change_timestamp','>=',$startTime)
                ->where('change_timestamp','<=',$endTime)
                ->first();
        $res = self::objToArray($res);
        return $res;    
    }

    //获取用户上周dd值
    public function getLastWeek($userId, $startWeek, $endWeek){
        $data = DB::table($this->table)->where('week',$startWeek.'-'.$endWeek)->where('user_id',$userId)->first();
        $data = self::objToArray($data);
        return $data;
    }

    //获取dd数据详情by id
    public function getLastWeekById($dataId){
        $data = DB::table($this->table)->where('id',$dataId)->first();
        $data = self::objToArray($data);
        return $data;
    }

    //更改数据
    public function changeData($data,$dataId){
        $data = DB::table($this->table)->where('id',$dataId)->update($data);
        return $data;
    }

    //新增数据
    public function insertData($data){
        $data = DB::table($this->table)->insert($data);
        return $data;
    }
}